<?php


class messageApp extends BackendApp {
    

    var $_message_mod;

    function __construct() {
        $this->MessageApp();
    }

    function MessageApp() {
        parent::BackendApp();
        $this->_message_mod = & m('message');
    }

    function index() {
        $conditions = '';
          // 处理分页
        $page = $this->_get_page(20);
        $coupon_codes_list = $this->_message_mod->find(array(
            'fields' => 'message.*,member.user_name,member.phone_mob',
            'conditions' => "message.from_id = 0 " . $conditions,
            'join' => 'received_belongs_to_member',
            'order' => "add_time desc",
            'limit' => $page['limit'],
            'count' => true,
        ));
        $page['item_count'] = $this->_message_mod->getCount();
        $this->_format_page($page);
        $this->assign('filtered', $conditions ? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
        $this->assign('coupon_list', $coupon_codes_list);
        // 引入前端资源
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        // 显示模板
        $this->display('message.index.html');
    }

    //新增
    function add(){
        if(IS_POST){
            $postData = $_POST;
            if(!$postData['sendcontent']){
                $this->show_warning('请填写要发送的内容！');
                return;
            }
            if(!$postData['user_id']){
                $this->show_warning('请选择发送人！');
                return;
            }
            $user_id = $postData['user_id'];
            $user_ids = explode(',',$user_id);
            $time = time();

            $sql = "INSERT INTO ecm_message (from_id,to_id,title,content,add_time,last_update,new,parent_id,status) VALUES ";
            $count = count($user_ids);
            foreach($user_ids as $k=>$v){
                if($count-1 == $k){
                    $sql .= "(0,$v,'系统消息','".$postData['sendcontent']."',$time,$time,0,0,3)";
                }else{
                    $sql .= "(0,$v,'系统消息','".$postData['sendcontent']."',$time,$time,0,0,3),";
                }
            }
             if (!$article_id = $this->_message_mod->db->query($sql)) {
                $this->show_warning('新增失败！');
                return;
            } else {
                $this->admin_log("新增站内信[" . $postData['sendcontent'] . "]", 1);
            }
            $this->show_message('新增成功', 'back_list', 'index.php?app=message', 'continue_add', 'index.php?app=message&amp;act=add'
            );
        }else{
            $this->display('message.form.html');
        }
    }
}